//
//  NewsDetViewController.m
//  LoveCar
//
//  Created by Du Bin on 15/5/30.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "NewsDetViewController.h"
#import "UMSocial.h"
#import <AFNetworkActivityIndicatorManager.h>

@interface NewsDetViewController ()

@property (nonatomic, retain) NSURL *url;
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) UIImageView *img;


@end

@implementation NewsDetViewController
- (void)dealloc
{
    [_id release];
    [_index release];
    [_webUrl release];
    [_share release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.id = [NSString string];
    }
    return self;
}

- (void)receiveId
{
    if ([self.index isEqualToString:@"1"] || [self.index isEqualToString:@"11"] ) {
        self.url = [NSURL URLWithString:[NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.0/content/news/videopage-a2-pm1-v4.0.0-vid%@-night0-showpage1-fs0.html", self.id]];
    } else if ([self.index isEqualToString:@"12"]) {
        self.url = [NSURL URLWithString:[NSString stringWithFormat:@"http://shuoke.autohome.com.cn/article/%@.html", self.id]];
    } else {
        self.url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.autohome.com.cn/news/201407/%@.html", self.id]];
    }
    
}

- (void)loadView
{
    [super loadView];
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.bounds.size.height - 20)];
    [self.view addSubview:self.webView];
    
    [self receiveId];
    NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
    
    [self.webView loadRequest:request];
    
    [self followScrollView: self.webView];
    [self.webView release];
    
    self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
    [self.img setImage:[UIImage imageNamed:@"share.png"]];
    UIBarButtonItem *itm = [[UIBarButtonItem alloc] initWithCustomView:self.img];
    self.navigationItem.rightBarButtonItem = itm;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(logAciton:)];
    [self.img addGestureRecognizer:tap];
    [self.img release];
    [itm release];
    [tap release];
    
}


- (void)logAciton:(id)sender
{
    BOOL isOauth = [UMSocialAccountManager isOauthWithPlatform:UMShareToSina];
    
    if(isOauth == YES){
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"5567f9fc67e58e99ee000633"
                                          shareText:[NSString stringWithFormat:@"%@ \n来自爱评车分享", self.url]//dic 的title
                                         shareImage:self.webView                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent, nil]
                                           delegate:nil];
//        if (response.responseCode == UMSResponseCodeSuccess) {
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"成功" message:@"分享成功" delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
//            [alertView show];
//        } else {
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"抱歉" message:@"分享失败" delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
//            [alertView show];
//        }
    } else{
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            //          获取微博用户名、uid、token等
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                
                //            UMSocialResponseEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
                //            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            }});
        
        //获取accestoken以及新浪用户信息，得到的数据在回调Block对象形参respone的data属性
        [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
            NSLog(@"SnsInformation is %@",response.data);
        }];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
